Online-Academy
Look, Read, Understand, Apply

Import content


# save this file in vehicle.py
class Vehicle:
    model:str = None
    def __init__(self,name,price,mod): # __init__ method is like a constructure method in Java, C++, it is 
           # used to initialize variables of object and called at the time of instance (object) creation.
        self.name = name  #self method refers to object, it is standard convention to write self, but it is not     #a keyword, instead of self we can write other words, except keywords of python, in methods of class. 
        self.price = price
        self.model = mod
    def get_name(self):  #self must be the first parameter to methods of class
        return self.name
    def get_price(self):
        return self.price
    def get_model(self):
        return self.model

#save this file in vehicle_import.py
#This file is importing vehicle.py. Using import keyword, we can use content defined in 
# one to another file. 
# here from is keyword,  vehicle is file name, import is keyword and Vehicle is class defined in file #vehicle.py
from vehicle import Vehicle
if __name__== "__main__":
    v = Vehicle("Volvo",4000,"i30")
    print(f"{v.get_name()}, {v.get_price()}")
    v.name = "Hyunda"

    v.model = "i20"
    print(f"{v.name}, {v.model}")
    Vehicle.name = "Mercedes"
    Vehicle.price = 300000
    Vehicle.model = "Scorpion"
    print(f"{Vehicle.name}, {Vehicle.price}")